package org.dromara.web.plugins.provider.impl;

import lombok.extern.slf4j.Slf4j;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.dao.business.domain.Model;
import org.dromara.web.controller.app.req.ChatBody;
import org.dromara.web.plugins.provider.ChatCompletionListener;
import org.dromara.web.plugins.provider.ChatProvider;
import org.dromara.web.plugins.provider.SsEmitterResponseBody;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;


@Component
@Slf4j
public class ChatProviderMai implements ChatProvider {

    public static final String MODEL_TYPE = "mai";

    @Override
    public void chat(ChatBody body, String model, SseEmitter emitter, ChatCompletionListener listener, boolean mute) {

       try {
           emitter.send(JsonUtils.toJsonString(SsEmitterResponseBody
               .builder()
               .content("您好")
               .sessionId(body.getSessionId())
               .appLocale(body.getAppLocale())
               .status(SsEmitterResponseBody.STATUS_OK)
               .role("assist")
               .build()));
           Thread.sleep(1000);
           emitter.send(JsonUtils.toJsonString(SsEmitterResponseBody
               .builder()
               .content(",")
               .sessionId(body.getSessionId())
               .appLocale(body.getAppLocale())
               .status(SsEmitterResponseBody.STATUS_OK)
               .role("assist")
               .build()));
           Thread.sleep(1000);

           emitter.send(JsonUtils.toJsonString(SsEmitterResponseBody
               .builder()
               .content("有")
               .sessionId(body.getSessionId())
               .appLocale(body.getAppLocale())
               .status(SsEmitterResponseBody.STATUS_OK)
               .role("assist")
               .build()));
           Thread.sleep(1000);

           emitter.send(JsonUtils.toJsonString(SsEmitterResponseBody
               .builder()
               .content("什么")
               .sessionId(body.getSessionId())
               .appLocale(body.getAppLocale())
               .status(SsEmitterResponseBody.STATUS_OK)
               .role("assist")
               .build()));
           Thread.sleep(1000);

           emitter.send(JsonUtils.toJsonString(SsEmitterResponseBody
               .builder()
               .content("可以")
               .sessionId(body.getSessionId())
               .appLocale(body.getAppLocale())
               .status(SsEmitterResponseBody.STATUS_OK)
               .role("assist")
               .build()));
           Thread.sleep(1000);

           emitter.send(JsonUtils.toJsonString(SsEmitterResponseBody
               .builder()
               .content("帮助")
               .sessionId(body.getSessionId())
               .appLocale(body.getAppLocale())
               .status(SsEmitterResponseBody.STATUS_OK)
               .role("assist")
               .build()));
           Thread.sleep(1000);

           emitter.send(JsonUtils.toJsonString(SsEmitterResponseBody
               .builder()
               .content("您?")
               .sessionId(body.getSessionId())
               .appLocale(body.getAppLocale())
               .status(SsEmitterResponseBody.STATUS_OK)
               .role("assist")
               .build()));
           Thread.sleep(1000);
           emitter.complete();
       } catch (Exception e) {
           listener.onCompletion("您好，有什么可以帮助您?", "assist");
       }
    }

    @Override
    public String chatResponse(Model model, String question) {


        return "您好，有什么问题可以帮您?";
    }
}
